<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加 -->
      <el-row :gutter="15">
        <el-col :span="9">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="ID" prop="order_id" width="60px"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="100px"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status" width="100px">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else >已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="100px"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditDialog(scope.row.order_id)">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button type="success" size="mini" icon="el-icon-location" @click="showProgress(scope.row.order_id)">定位</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <!--  @size-chang : 监听 pagesize 改变的事件
        @current-change : 监听 页码值 改变的事件
       -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 卡片视图区域结束 -->
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑订单" :visible.sync="dialogVisible" width="50%" @close="dialogClosed">
      <el-form :model="orderForm" :rules="orderFormRules" ref="orderFormRef" label-width="100px">
        <el-form-item label="ID" prop="order_id">
          <el-input v-model="orderForm.order_id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="订单编号" prop="order_number">
          <el-input v-model="orderForm.order_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="订单价格" prop="order_price">
          <el-input v-model="orderForm.order_price" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="是否付款" prop="pay_status">
          <el-input v-model="orderForm.pay_status" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="是否发货" prop="is_send">
          <el-input v-model="orderForm.is_send" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="下单时间" prop="create_time">
          <!-- <span> {{ orderForm.create_time | dateFormat }} </span> -->
          <el-input v-model="create_time" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="orderForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="orderForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 展示物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <!-- timeline 事件线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressList"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './city.data-3.js'
import dateFmt from './dateFmt.js'
import kuaidi from './kuaidi.js'
export default {
  data() {
    return {
      // 搜索字段
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 表格数据
      tableData: [],
      // 表格总条数
      total: 0,
      // 编辑对话框显示隐藏
      dialogVisible: false,
      // 表单
      orderForm: {
        order_id: '',
        order_number: '',
        order_price: '',
        pay_status: '',
        is_send: '',
        create_time: '',
        createTime: '',
        address1: [],
        address2: ''
      },
      // 表单规则
      orderFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      cityData: cityData,
      dateFmt: dateFmt,
      kuaidi: kuaidi,
      // 物流进度显示隐藏
      progressVisible: false,
      // 物流信息数据
      progressList: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 订单表格
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.tableData = res.data.goods
      this.total = res.data.total
      // this.$message.success(res.meta.msg)
    },
    // 分页 - 分页条数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 分页 - 下一页页码
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 编辑按钮事件
    async showEditDialog(orderId) {
      const { data: res } = await this.$http.get(`orders/${orderId}`)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.orderForm.order_id = res.data.order_id
      this.orderForm.order_number = res.data.order_number
      this.orderForm.order_price = res.data.order_price
      if (res.data.pay_status === '0') {
        this.orderForm.pay_status = '未付款'
      } else {
        this.orderForm.pay_status = '已付款'
      }
      if (res.is_send === '0') {
        this.orderForm.is_send = '未发货'
      } else {
        this.orderForm.is_send = '已发货'
      }
      this.orderForm.createTime = res.data.create_time
      this.dialogVisible = true
    },
    dialogClosed() {
      this.$refs.orderFormRef.resetFields()
    },
    // 物流定位按钮事件
    async showProgress(orderId) {
      const res = this.kuaidi
      // console.log(res)
      this.progressList = res.data
      this.progressVisible = true
      // console.log(this.progressList)
    }
  },
  // 计算属性
  computed: {
    // input数据双向绑定时间戳转换日期
    create_time: {
      get() {
        // 传入需要格式化的值,跟data里的值对应
        const date = new Date(this.orderForm.createTime)
        // 调用刚刚写好的js代码格式化
        return dateFmt.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    }
  }
}
</script>

<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';

.el-cascader{
  width: 100%;
}
</style>
